package com.lottery.im.module.result.presenter;

import android.app.Activity;
import com.lottery.im.MyApplication;
import com.lottery.im.api.ApiClient;
import com.lottery.im.api.RetryWithDelay;
import com.lottery.im.api.observer.BaseObserver;
import com.lottery.im.api.observer.ShowTipObserver;
import com.lottery.im.api.result.entity.EmptyData;
import com.lottery.im.api.result.entity.FictitiousMap;
import com.lottery.im.api.result.entity.GsUserInfo;
import com.lottery.im.api.result.entity.SaleGameInfo;
import com.lottery.im.base.BaseFragmentPresenter;
import com.lottery.im.module.result.view.GsPlanDetailView;
import com.trello.rxlifecycle2.LifecycleProvider;
import com.trello.rxlifecycle2.android.FragmentEvent;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import java.util.concurrent.TimeUnit;

public class GsPlanDetailPresenter extends BaseFragmentPresenter<GsPlanDetailView> {

  public GsPlanDetailPresenter(Activity activity,
      GsPlanDetailView iView,
      LifecycleProvider<FragmentEvent> provider) {
    super(activity, iView, provider);
  }

  /**
   * 获取高手计划用户信息
   * @param id 用户ID
   */
  public void getById(int id) {
    ApiClient.getApiRetrofitInstance()
        .getById(id, MyApplication.getInstance().getUserInfo().getUserId(),
            MyApplication.getInstance().getToken())
        .retryWhen(new RetryWithDelay(100, 50000))
        .throttleFirst(2, TimeUnit.SECONDS)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .compose(provider.bindToLifecycle())
        .subscribe(new BaseObserver<GsUserInfo>() {

          @Override public void onStart() {

          }

          @Override public void onSuccess(GsUserInfo userInfo) {
            iView.getByIdSuc(userInfo);
          }

          @Override public void onFail(String code, String msg) {

          }

          @Override public void onException(String msg) {

          }

          @Override public void onFinish() {

          }
        });
  }

  /**
   * 点赞
   * @param id 用户ID
   */
  public void good(int id) {
    ApiClient.getApiRetrofitInstance()
        .good(id, MyApplication.getInstance().getUserInfo().getUserId(),
            MyApplication.getInstance().getToken())
        .retryWhen(new RetryWithDelay(100, 50000))
        .throttleFirst(2, TimeUnit.SECONDS)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .compose(provider.bindToLifecycle())
        .subscribe(new ShowTipObserver<EmptyData>(activity){
          @Override public void onSuccess(EmptyData emptyData) {
            iView.goodSuc();
          }
        });
  }

  /**
   * 高手计划最近两期开奖情况
   * @param gameType 类型
   */
  public void querySaleGameInfo(String gameType) {
    ApiClient.getApiRetrofitInstance()
        .querySaleGameInfo(gameType)
        .retryWhen(new RetryWithDelay(100, 50000))
        .throttleFirst(2, TimeUnit.SECONDS)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .compose(provider.bindToLifecycle())
        .subscribe(new BaseObserver<SaleGameInfo>() {

          @Override public void onStart() {

          }

          @Override public void onSuccess(SaleGameInfo gameInfo) {
            iView.querySaleGameInfoSuc(gameInfo);
          }

          @Override public void onFail(String code, String msg) {

          }

          @Override public void onException(String msg) {

          }

          @Override public void onFinish() {

          }
        });
  }

  /**
   * 预测结果
   * @param id 用户ID
   * @param limit 期数
   */
  public void getFictitiousRecord(int id,int limit) {
    ApiClient.getApiRetrofitInstance()
        .getFictitiousRecord(id,limit, MyApplication.getInstance().getUserInfo().getUserId(),
            MyApplication.getInstance().getToken())
        .retryWhen(new RetryWithDelay(100, 50000))
        .throttleFirst(2, TimeUnit.SECONDS)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .compose(provider.bindToLifecycle())
        .subscribe(new BaseObserver<FictitiousMap>() {

          @Override public void onStart() {

          }

          @Override public void onSuccess(FictitiousMap fictitiousMap) {
            iView.getFictitiousRecordSuc(fictitiousMap);
          }

          @Override public void onFail(String code, String msg) {

          }

          @Override public void onException(String msg) {

          }

          @Override public void onFinish() {

          }
        });
  }

}
